package IA;

import iterators.AvailableTableItr;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Christopher
 */
public class TableCollection {

    private ArrayList<Table> tables;
    private int totalNumberOfTables = 10;

    public TableCollection() {
        tables = new ArrayList<Table>();

        for (int i = 0; i < totalNumberOfTables; i++) {
            tables.add(new Table(4, i));
        }
    }

    public Table getTable(int index) {
        return tables.get(index);
    }

    public Iterator<Table> getAvailTableIterator() {
        return new AvailableTableItr(tables);
    }

    public Boolean getTableStatus(Integer index) {
        return tables.get(index).getIsAvailable();
    }

    public Boolean TablesOpen() {
        Iterator itr = this.getAvailTableIterator();
        return itr.hasNext();
    }

    public void assignTable(Integer index, Boolean isAvailable) {
        tables.get(index).setIsAvailable(isAvailable);
    }

    public int getTotalNumberOfTables() {

        return totalNumberOfTables;
    }

    public void setTotalNumberOfTables(int totalNumberOfTables) {
        int diff = totalNumberOfTables - this.totalNumberOfTables;
        if (diff > 0) {
            addTables(diff);
        } else {
            if (diff < 0) {
                deleteTables(Math.abs(diff));
            }
        }
        this.totalNumberOfTables = totalNumberOfTables;
    }

    public void addTables(Integer tablesToAdd) {
        for (int i = 0; i < tablesToAdd; i++) {
            tables.add(new Table(4, tables.size() + i));
        }
    }

    public void deleteTables(Integer tablesToDelete) {
        for (int i = 0; i < tablesToDelete; i++) {
            tables.remove(tables.size() - 1);
        }
    }
}
